//
//  SecurityUtil.h
//  Smile
//
//  Created by apple on 15/8/25.
//  Copyright (c) 2015年 Weconex. All rights reserved.
//

#import "SecurityUtil.h"
#import "GTMBase64.h"
#import "NSData+AES.h"

#define Iv          @"DxDg3HJkLZx4vbn8" //偏移量,可自行修改
#define KEY         @"DxDg3HJkLZx4vbn8" //key，可自行修改

#pragma mark - 日志开关
#ifdef DEBUG
#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define DLog(...)
#endif

@implementation SecurityUtil

#pragma mark - base64
+ (NSString*)encodeBase64String:(NSString * )input { 
    NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 
    data = [GTMBase64 encodeData:data]; 
    NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
	return base64String;
    
}

+ (NSString*)decodeBase64String:(NSString * )input { 
    NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 
    data = [GTMBase64 decodeData:data]; 
    NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
	return base64String;
} 

+ (NSString*)encodeBase64Data:(NSData *)data {
	data = [GTMBase64 encodeData:data]; 
    NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
	return base64String;
}

+ (NSString*)decodeBase64Data:(NSData *)data {
	data = [GTMBase64 decodeData:data]; 
    NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
	return base64String;
}

#pragma mark - AES加密
//将string转成带密码的data
+(NSString*)encryptAESData:(NSString*)string
{
    //将nsstring转化为nsdata
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    //使用密码对nsdata进行加密
    NSData *encryptedData = [data AES128EncryptWithKey:KEY gIv:Iv];
    //返回进行base64进行转码的加密字符串
    return [self encodeBase64Data:encryptedData];
}

#pragma mark - AES解密
//将带密码的data转成string
+(NSString*)decryptAESData:(NSString *)string
{
    //base64解密
    NSData *decodeBase64Data=[GTMBase64 decodeString:string];
    //使用密码对data进行解密
    NSData *decryData = [decodeBase64Data AES128DecryptWithKey:KEY gIv:Iv];
    //将解了密码的nsdata转化为nsstring
    NSString *str = [[NSString alloc] initWithData:decryData encoding:NSUTF8StringEncoding];
    return str;
}

#pragma mark    自定义方法,把请求到的加密数据转化为字典
//把请求到的加密数据转化为字典
+ (NSDictionary *)transformResponseObject:(id)responseObject
{
    NSData * responseData   =   (NSData *)responseObject;
    
    //将请求的data数据转化为Str
    NSString * responseStr  =   [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
    
    //加密
    //        NSString *encryptDate=[SecurityUtil encryptAESData:@"abc123"];
    
    //解密,
    NSString * jsonStr      =   [SecurityUtil decryptAESData:responseStr];
    
    DLog(@"%@",jsonStr);
    
    NSData * jsonData =   [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary * jsonDic  =   [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    return jsonDic;
}

#pragma mark    自定义方法,把字典转化为jsonStr后在用AES加密
+ (NSString *)transformDictionaryToAES128JsonStr:(NSDictionary *)dic
{
    NSString * jsonStr  =   [SecurityUtil transformDictionaryToJsonStr:dic];
    NSString * jsonAES128   =   [SecurityUtil encryptAESData:jsonStr];
    DLog(@"jsonAES128==%@",jsonAES128);
    return jsonAES128;
}

#pragma mark    把字典转化为JSonStr
+ (NSString*)transformDictionaryToJsonStr:(NSDictionary *)dic
{
    NSError *parseError = nil;
    
    //options 不设置得到无格式的json
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:&parseError];
    NSString * jsonStr  =   [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    return jsonStr;
}


@end
